package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AdminWeixinUser;
import com.ruoyi.system.mapper.AdminWeixinUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdminAgentRobotMapper;
import com.ruoyi.system.domain.AdminAgentRobot;
import com.ruoyi.system.service.IAdminAgentRobotService;

/**
 * 代理机器人管理Service业务层处理
 *
 * @author ruoyi
 * @date 2020-11-19
 */
@Service
public class AdminAgentRobotServiceImpl implements IAdminAgentRobotService
{
    @Autowired
    private AdminAgentRobotMapper adminAgentRobotMapper;

    @Autowired
    private AdminWeixinUserMapper adminWeixinUserMapper;

    /**
     * 查询代理机器人管理
     *
     * @param id 代理机器人管理ID
     * @return 代理机器人管理
     */
    @Override
    public AdminAgentRobot selectAdminAgentRobotById(Long id)
    {
        AdminAgentRobot adminAgentRobot = adminAgentRobotMapper.selectAdminAgentRobotById(id);
        if (adminAgentRobot==null) {
            return adminAgentRobot;
        }
        if(StringUtils.isNotBlank(adminAgentRobot.getBeginTime()) && StringUtils.isNotBlank(adminAgentRobot.getEndTime())){
            String[] s= new String[2];
            s[0]=adminAgentRobot.getBeginTime();
            s[1]=adminAgentRobot.getEndTime();
            adminAgentRobot.setFromTo(s);
        }
        return adminAgentRobot;
    }

    /**
     * 查询代理机器人管理列表
     *
     * @param adminAgentRobot 代理机器人管理
     * @return 代理机器人管理
     */
    @Override
    public List<AdminAgentRobot> selectAdminAgentRobotList(AdminAgentRobot adminAgentRobot)
    {
        return adminAgentRobotMapper.selectAdminAgentRobotList(adminAgentRobot);
    }

    /**
     * 新增代理机器人管理
     *
     * @param adminAgentRobot 代理机器人管理
     * @return 结果
     */
    @Override
    public int insertAdminAgentRobot(AdminAgentRobot adminAgentRobot)
    {
        String wexinId = getRandomString(14);
        adminAgentRobot.setWexinId(wexinId);
        adminAgentRobot.setCreateTime(DateUtils.getNowDate());
        AdminWeixinUser adminWeixinUser = new AdminWeixinUser();
        adminWeixinUser.setRoom(adminAgentRobot.getRoom());
        adminWeixinUser.setWexinId(wexinId);
        AdminWeixinUser weixinUser = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(adminWeixinUser);
        if (weixinUser==null) {
            adminWeixinUser.setWexinName(adminAgentRobot.getWexinName());
            adminWeixinUser.setIsRobot(1);
            adminWeixinUserMapper.insertAdminWeixinUser(adminWeixinUser);
        }
        return adminAgentRobotMapper.insertAdminAgentRobot(adminAgentRobot);
    }

    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer("robot_");
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 修改代理机器人管理
     *
     * @param adminAgentRobot 代理机器人管理
     * @return 结果
     */
    @Override
    public int updateAdminAgentRobot(AdminAgentRobot adminAgentRobot)
    {
        adminAgentRobot.setUpdateTime(DateUtils.getNowDate());
        AdminAgentRobot adminAgentRobot1 = adminAgentRobotMapper.selectAdminAgentRobotById(adminAgentRobot.getId());
        //本来的微信信息
        AdminWeixinUser weixinUser = new AdminWeixinUser();
        weixinUser.setRoom(adminAgentRobot1.getRoom());
        weixinUser.setWexinId(adminAgentRobot1.getWexinId());
        AdminWeixinUser adminWeixinUser = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(weixinUser);
        AdminWeixinUser updateWeixinUser = new AdminWeixinUser();
        updateWeixinUser.setUpdateTime(DateUtils.getNowDate());
        updateWeixinUser.setRoom(adminAgentRobot.getRoom());
        updateWeixinUser.setWexinName(adminAgentRobot.getWexinName());
        updateWeixinUser.setId(adminWeixinUser.getId());

        adminWeixinUserMapper.updateAdminWeixinUser(updateWeixinUser);
        return adminAgentRobotMapper.updateAdminAgentRobot(adminAgentRobot);
    }

    /**
     * 批量删除代理机器人管理
     *
     * @param ids 需要删除的代理机器人管理ID
     * @return 结果
     */
    @Override
    public int deleteAdminAgentRobotByIds(Long[] ids)
    {
        for (Long id:ids) {
            AdminAgentRobot adminAgentRobot1 = adminAgentRobotMapper.selectAdminAgentRobotById(id);
            AdminWeixinUser weixinUser = new AdminWeixinUser();
            weixinUser.setRoom(adminAgentRobot1.getRoom());
            weixinUser.setWexinId(adminAgentRobot1.getWexinId());
            AdminWeixinUser adminWeixinUser = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(weixinUser);
            adminWeixinUserMapper.deleteAdminWeixinUserById(adminWeixinUser.getId());
        }
        return adminAgentRobotMapper.deleteAdminAgentRobotByIds(ids);
    }

    /**
     * 删除代理机器人管理信息
     *
     * @param id 代理机器人管理ID
     * @return 结果
     */
    @Override
    public int deleteAdminAgentRobotById(Long id)
    {
        AdminAgentRobot adminAgentRobot1 = adminAgentRobotMapper.selectAdminAgentRobotById(id);
        AdminWeixinUser weixinUser = new AdminWeixinUser();
        weixinUser.setRoom(adminAgentRobot1.getRoom());
        weixinUser.setWexinId(adminAgentRobot1.getWexinId());
        AdminWeixinUser adminWeixinUser = adminWeixinUserMapper.selectAdminWeixinUserByWeChatIdAndRoom(weixinUser);
        adminWeixinUserMapper.deleteAdminWeixinUserById(adminWeixinUser.getId());
        return adminAgentRobotMapper.deleteAdminAgentRobotById(id);
    }
}
